
cc.Class({
    extends: cc.Component,

    properties: {
        playerNode: cc.Node,
        enemyNode: cc.Node,
        boomNode: cc.Node,
        scoreLabel: cc.Label,
        homeBtn: cc.Button,
    },


    onLoad () {
        this.score = 0;

        this.placePlayer();
        this.placeEnemy();


        this.node.on('touchstart', this.fire, this);
        this.homeBtn.node.on('click', this.toHome, this)
    },

    // 返回首页
    toHome() {
        cc.director.loadScene('index')
    },

    onDestroy() {
        this.node.off('touchstart', this.fire, this);
        clearTimeout(this.timeTaskDie)

    },

    // start () {

    // },

    update (dt) {
        // 成功撞击
        if(this.playerNode.position.sub(this.enemyNode.position).mag() < this.playerNode.width / 2 + this.enemyNode.width / 2) {
            this.enemyNode.active = false;
            this.boom(this.enemyNode.position, this.enemyNode.color);

            this.playerNode.stopAction(this.playerRunAction);
            this.playerNode.stopAction(this.playerRunAction2);
            this.enemyNode.stopAction(this.enemyNodeRunAction);

            // 得分
            this.scoreLabel.string = ++this.score;

            // 重置玩家和敌人
            this.placePlayer();
            this.placeEnemy();

        }
    },


    placePlayer() {

        this.playerNode.active = true;
        this.isFire = false;
        this.playerNode.y = - cc.winSize.height / 4;

        // 自主掉落
        let dua = 10;
        let seq = cc.sequence(
            cc.moveTo(dua, cc.v2(this.playerNode.x, -(cc.winSize.height /2 - this.playerNode.height/2))),
            cc.callFunc(() => {
                this.die()
            })
        );

        this.playerRunAction2 = this.playerNode.runAction(seq);
    },

    placeEnemy() {
        this.enemyNode.active = true;

        let x = cc.winSize.width / 2 - this.enemyNode.width / 2;
        let y = cc.winSize.height / 4 * Math.random();
        let dua = 1 + Math.random() * 0.5;

        this.enemyNode.x = 0;
        this.enemyNode.y = cc.winSize.height / 3 - this.enemyNode.height/2;

        let seq = cc.repeatForever(
            cc.sequence(
                cc.moveTo(dua, -x, y),
                cc.moveTo(dua, x, y),
            )
        );

        this.enemyNodeRunAction = this.enemyNode.runAction(seq);

    },


    fire() {
        if(this.isFire) {
            return
        }
        this.isFire = true;
        let dua = 0.6;
        let seq = cc.sequence(
            cc.moveTo(dua, cc.v2(0, cc.winSize.height / 2)),
            cc.callFunc(() => {
                this.die()
            })
        )

        this.playerRunAction = this.playerNode.runAction(seq);
    },

    // die
    die() {

        this.playerNode.active = false;
        this.boom(this.playerNode.position, this.playerNode.color)

        this.timeTaskDie = setTimeout(() => {
            cc.director.loadScene('diandian');
        }, 1000);

    },

    boom(pos, color) {
        this.boomNode.setPosition(pos);
        let particle = this.boomNode.getComponent(cc.ParticleSystem);
        if(color) {
            particle.startColor = particle.endColor = color;
        }
        particle.resetSystem();
    }

});
